package com.pingbu.tijian.hospital.backapi.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pingbu.common.bean.ResultBean;
import com.pingbu.common.utils.PbSecretUtils;
import com.pingbu.tijian.hospital.backapi.bean.entity.Student;
import com.pingbu.tijian.hospital.backapi.bean.entity.StudentGroup;
import com.pingbu.tijian.hospital.backapi.bean.entity.UserInfo;
import com.pingbu.tijian.hospital.backapi.mapper.StudentGroupMapper;
import com.pingbu.tijian.hospital.backapi.mapper.StudentMapper;
import com.pingbu.tijian.hospital.backapi.mapper.UserInfoMapper;
import org.apache.catalina.User;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {

    private static Logger logger = LoggerFactory.getLogger(StudentService.class);

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private StudentGroupMapper studentGroupMapper;
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 学生列表分页
     * @param param
     * @return
     */
    public ResultBean<PageInfo<Student>> page(Student param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<Student> lst1 = studentMapper.list(param);
        PageInfo<Student> pi1 = new PageInfo<>(lst1);
        PageHelper.clearPage();

        refreshExtendField(lst1);

        return ResultBean.create(0, "success", pi1);
    }

    /**
     * 获取所有学生
     * @param param
     * @return
     */
    public ResultBean<List<Student>> list(Student param) {
        List<Student> lst1 = studentMapper.list(param);
        refreshExtendField(lst1);

        return ResultBean.create(0, "success", lst1);
    }



    /**
     * 刷新扩展字段
     * @param lst1
     */
    public void refreshExtendField(List<Student> lst1){
        // 遍历更新学生所属组的组名称
        for (Student st : lst1) {
            StudentGroup sg1 = new StudentGroup();
            sg1.setNoid(st.getStudent_group_id());
            StudentGroup sg2 = studentGroupMapper.get(sg1);
            if (sg2 != null) {
                st.setStudentGroupName(sg2.getStudent_group_name());
            }
        }

        // 完善 ，遍历 lst1 里面的 create_user_id ，更新 createUserRealname
        for (Student st : lst1) {
            UserInfo ui1 = new UserInfo();
            ui1.setNoid(st.getCreate_user_id());
            UserInfo ui2 = userInfoMapper.get(ui1);
            if (ui2 != null) {
                st.setCreateUserRealname(ui2.getRealname());
            }
        }
    }

    /**
     * 增加一个学生
     */
    public ResultBean<Integer> add(Student param) {

        //
        param.setNoid(PbSecretUtils.uuid());

        try {
            studentMapper.insert(param);
        } catch (Exception err) {
            return ResultBean.create(23, err.getMessage());
        }
        return ResultBean.create(0, "success");
    }

    /**
     * 修改一个学生
     * @param param
     * @return
     */
    public ResultBean<Integer> update(Student param) {
        Student ui1 = new Student();
        ui1.setNoid(param.getNoid());
        ui1.setStudent_code(param.getStudent_code());
        ui1.setStudent_realname(param.getStudent_realname());
        ui1.setStudent_sex(param.getStudent_sex());
        ui1.setStudent_age(param.getStudent_age());
        ui1.setOrder_numb(param.getOrder_numb());
        ui1.setBind_usercode(param.getBind_usercode());
        ui1.setStudent_group_id(param.getStudent_group_id());
        ui1.setBind_usercode(param.getBind_usercode());
        Integer res2 = studentMapper.update(ui1);
        return ResultBean.create(0, "success", res2);
    }

    /**
     * 删除一个学生
     * @param param
     * @return
     */
    public ResultBean<Integer> delete(Student param){
        Integer res = studentMapper.delete(param);
        return ResultBean.create(0, "success", res);

    }

}
